/**
 * 
 */
package dms.dto;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import dms.model.School;
import dms.service.SchoolService;
import dms.util.ContextUtil;

/**
 * @author 李健
 *
 */
public class FacultyDTO extends ActionForm {
	private Long id;
	private String name;
	private String description;
	private Long school;
	/**
	 * @return the id
	 */
	public Long getId() {
		return id;
	}
	/**
	 * @param id the id to set
	 */
	public void setId(Long id) {
		this.id = id;
	}
	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}
	/**
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}
	/**
	 * @param description the description to set
	 */
	public void setDescription(String description) {
		this.description = description;
	}
	/**
	 * @return the school
	 */
	public Long getSchool() {
		return school;
	}
	/**
	 * @param school the school to set
	 */
	public void setSchool(Long school) {
		this.school = school;
	}
	
	@Override
	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
		ActionErrors errors = new ActionErrors();
		
		if (this.description == null || description.length() == 0) {
			errors.add("errors", new ActionMessage("缺少科系介绍", false));
		}
		
		if (this.name == null || this.name.length() == 0) {
			errors.add("errors", new ActionMessage("缺少科系名称", false));

		}
		
		if (this.school == null || this.school == 0) {
			errors.add("errors", new ActionMessage("没有选择学院", false));

		}
		
		if (errors.size() != 0 && this.school != null) {
			SchoolService schoolService = (SchoolService) ContextUtil.getContext().getBean("schoolService");
			School school = schoolService.findSchoolById(this.school);
			request.setAttribute("school", school);
		}
		return errors;
		
	}
	
	@Override
	public void reset(ActionMapping mapping, HttpServletRequest request) {
		this.name = null;
		this.description = null;
		this.school = null;
		this.id = null;
	}
}
